/**
 * Created by vicket on 2017/4/14.
 */
$(function(){
    var username=$("#user_name");
    var password=$("#password");
    var errorTip=$(".error_tip");
    var errorText=$(".error_text");
    var loginBtn=$(".login_btn");
    var isLogin=true;
    username.on("focus",function(){
        //errorText.hide();
        //errorTip.attr("src","../img/login.png");
        //errorText.text("");
    });
    username.on("keyup",function(){
        if(!/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(username.val())){
            isLogin=false;
            errorTip.attr("src","../public/images/login.png");
            errorText.show();
            errorText.text("只支持中文、数字、字母");
        }else{
            isLogin=true;
            errorTip.attr("src","../public/images/login.png");
            errorText.hide();
        }
    })
    username.on("blur",function(){
        if(username.val().length==0){
            isLogin=false;
            errorTip.attr("src","../public/images/login.png");
            errorText.show();
            errorText.text("用户名不能为空");
        }else{
            if(password.val().length==0){
                isLogin=false;
                errorTip.attr("src","../public/images/login.png");
                errorText.show();
                errorText.text("填写密码");
            }else{
                isLogin=true;
                errorTip.attr("src","../public/images/flogin.png");
                errorText.hide();
            }
        }
    });
    password.on("focus",function(){
        errorText.hide();
        errorTip.attr("src","../public/images/login.png");
        errorText.text("");
    });

    password.on("keyup",function(){
        if(!/^[0-9a-zA-Z_]{1,}$/.test(password.val())){
            isLogin=false;
            errorTip.attr("src","../public/images/login.png");
            errorText.show();
            errorText.text("仅支持数字、字母和下划线");
        }else{
            isLogin=true;
            errorText.hide();
        }
    })
    password.on("blur",function(){
        if(password.val().length==0){
            isLogin=false;
            errorTip.attr("src","../public/images/login.png");
            errorText.show();
            errorText.text("密码不能为空");

        }else if(password.val().length<8||password.val().length>20){
            isLogin=false;
            errorTip.attr("src","../public/images/login.png");
            errorText.show();
            errorText.text("8-20位数字或英文");

        }else{
            if(username.val().length==0){
                isLogin=false;
                errorTip.attr("src","../public/images/login.png");
                errorText.show();
                errorText.text("填写用户名");
            }else{
                isLogin=true;
                errorText.hide();
                errorTip.attr("src","../public/images/flogin.png");
            }
        }
    });
    loginBtn.tap(function(){
        var usernameVal=$("#user_name").val();
        var passwordVal=$("#password").val();
       if(usernameVal.length==0||passwordVal.length==0){
           isLogin=false;
           errorTip.attr("src","../public/images/login.png");
           errorText.show();
           errorText.text("昵称或密码不能为空");

       }else if(!isLogin){
           isLogin=false;
           errorTip.attr("src","../public/images/login.png");
           errorText.hide();
           errorText.text("");
       }else{
               errorTip.attr("src","../public/images/flogin.png");
               $.ajax({
                   type:'post',
                   url:"/api/frontLogin",
                   data:{username:usernameVal,password:passwordVal},
                   success:function(data){
                       if(data.code==1){
                           isLogin=true;
                           window.location.href="../home.ejs";
                           sessionStorage.setItem("username",usernameVal);
                           sessionStorage.setItem("userId",data.userId);
                       }else{
                           isLogin=false;
                           errorTip.attr("src","../public/images/login.png");
                           errorText.show();
                           errorText.text(data.message);
                       }

                   }
               })
           }


   });
})